package com.simplehc7878.auth.security.jwt;

import com.simplehc7878.auth.security.SecurityParams;
import com.simplehc7878.auth.security.ChainConfigure;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

public class JwtChainConfigure implements ChainConfigure {
    @Override
    public SecurityFilterChain configure(HttpSecurity http, SecurityParams params) throws Exception {
        http
                .csrf().disable()
                .authorizeRequests()
                .antMatchers("/user/auth/**").permitAll()
                .anyRequest().authenticated() // 其他接口需要认证
                .and()
                .exceptionHandling()
                .authenticationEntryPoint(params.getAuthenticationEntryPoint())
                .and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .addFilterBefore(params.getOncePerRequestFilter(), UsernamePasswordAuthenticationFilter.class);
        return http.build();
    }
}
